#include <iostream>
using namespace std;

void print1(int (*matrix)[4], int rowSize) {
    cout << "[" << endl;
    for(int i = 0; i < rowSize; i++) {
        cout << "  [ ";
        for(auto i : matrix[0]) {
            cout << i << " ";
        }
        cout << "]" << endl;
    }
    cout << "]" << endl;
}
void print2(int matrix[][4], int rowSize) {
    cout << "[" << endl;
    for(int i = 0; i < rowSize; i++) {
        cout << "  [ ";
        for(auto i : matrix[0]) {
            cout << i << " ";
        }
        cout << "]" << endl;
    }
    cout << "]" << endl;
}
void print3(int *p, int rowSize, int colSize) {
    int row = 0;
    cout << "[" << endl;
    while(row != rowSize) {
        // 计算第 row 行的首元素地址
        int *pRow = p + row * colSize;
        int col = 0;
        cout << "  [ ";
        while(col != colSize) {
            // 计算 第 row 行 第 col 列的地址
            int *pRowCol = pRow + col;
            cout << *pRowCol << " ";
            col++;
        }
        cout << "]" << endl;
        row++;
    }
    cout << "]" << endl;

}

void test() {
    int matrix[3][4] = {
        { 0, 1, 2, -1},
        { 3, 4, 5, -1 },
        { 6, 7, 8, -1 }
    };
    cout << "matrix 的大小" << sizeof(matrix) << endl;
    cout << "matrix[0] 的大小" << sizeof(matrix[0]) << endl;
    print1(matrix, sizeof(matrix)/sizeof(matrix[0]));
    print2(matrix, sizeof(matrix)/sizeof(matrix[0]));

    int (*p)[4] = new int[3][4]{
        { 6, 7, 8, -1 },
        { 3, 4, 5, -1 },
        { 0, 1, 2, -1}
    };
    print1(p, sizeof(int[3][4]) / sizeof(int[4]));
    delete [] p;

    // 二维数组也是内存上一块连续内存
    int *p2 = (int *)new int[3][4]{
        { 6, 7, 8, -1 },
        { 3, 4, 5, -1 },
        { 0, 1, 2, -1}
    };
    print3(p2, 3, 4);
    for (int i = 0; i < 3*4; i++) {
        cout << *p2++ << " ";
    }
    cout << endl;
}

int main() {
    test();
    return 0;
}